<template>
  <body id="paper">
  <el-form ref="form" :model="loginForm" :rules="rules" class="login-container" label-position="left"
           label-width="0px" v-loading="loading">
    <h3 class="login_title">凯翼MES新平台</h3>
    <el-form-item prop="username">
      <el-input type="text" v-model="loginForm.username"
                auto-complete="off" placeholder="用户"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input type="password" v-model="loginForm.password"
                auto-complete="off" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item>
      <div style="text-align:center">
        <el-button type="primary" style="width:40%" v-on:click="login">登录</el-button>
      </div>
    </el-form-item>
  </el-form>
  </body>
</template>
<script>
  export default{
    data () {
      return {
        rules: {
          username: [{required: true, message: '用户名不能为空', trigger: 'blur'}],
          password: [{required: true, message: '密码不能为空', trigger: 'blur'}]
        },
        checked: true,
        loginForm: {
          username: '',
          password: ''
        },
        loading: false
      }
    },
    mounted() {
      //单点直接登录
      if(this.$route.query && this.$route.query.username){
        this.loginForm.username = this.$route.query.username
      }
      if(this.$route.query && this.$route.query.password){
        this.loginForm.password = this.$route.query.password
      }
      if(this.$route.query.password && this.$route.query.username){
        this.login()
      }
      document.onkeydown=e=>{
        let _key=window.event.keyCode;
        if(_key===13){
          this.login()
        }
      }
    },
    methods: {
      login () {
        this.$refs.form.validate(valid => {
          if (valid) {
            const param = {
              username: this.loginForm.username,
              password: this.loginForm.password
            }
            this.$store.dispatch('login/login', param)
              .then(() => {
                this.$router.push({ path: '/' })
              })
              .catch(err => {

              })
          }
        })
      }
    }
  }
</script>
<style>
  #paper {
    background:url(~@/assets/images/logo-xj.jpg) no-repeat;
    height: 100%;
    width: 100%;
    background-size: cover;
    position: fixed;
  }
  body{
    margin: 0;
  }
  .login-container {
    border-radius: 15px;
    background-clip: padding-box;
    margin: 10% auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    /*background: #fff;*/
    background: rgba(255,255,255,0.5);
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
    position: relative;
    left: 30%;
  }
  .login_title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }
</style>
